home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / CTEX__ / PXTOPK.C < prev   
Text File  |  1987-05-25  |  19KB  |  668 lines

  1. /*  pxtopk.c by tomas rokicki */
  2. /* comment to avoid beastly number */
  3. #include <stdio.h>
  4. #define maxmemsize 200000 
  5. #define namelength 80 
  6. #define terminallinelength 132 
  7. #define lo(a) ((a)&65535)
  8. #define hi(a) lo((a)>>16)
  9. #define incr(a) a++
  10. #define decr(a) a--
  11. #define true (1)
  12. #define false (0)
  13. #define chr(a) a
  14. #define round(a) ((int)(a+.5))
  15.  
  16.   typedef int integer ;
  17.   typedef char quarterword ;
  18.   typedef char boolean ;
  19.   typedef float real ;
  20.   typedef quarterword ASCIIcode ; 
  21.   typedef FILE *textfile ; 
  22.   typedef quarterword eightbits ; 
  23.   typedef FILE *wordfile ; 
  24.   typedef FILE *bytefile ; 
  25.   ASCIIcode xord[128] ; 
  26.   char xchr[256] ; 
  27.   wordfile pxlfile ; 
  28.   bytefile pkfile ; 
  29.   char pxlname[81], pkname[81] ; 
  30.   integer pkloc ; 
  31.   integer bitweight ; 
  32.   integer outputbyte ; 
  33.   integer mem[200001] ; 
  34.   integer nextmemfree ; 
  35.   integer dirptr ; 
  36.   integer car ; 
  37.   integer hppp ; 
  38.   char comment[20] ; 
  39.   integer width ; 
  40.   integer height ; 
  41.   integer cxoff ; 
  42.   integer cyoff ; 
  43.   real pxlconv ; 
  44.   integer power[32] ; 
  45.   integer haveext ; 
  46.   integer lastext ; 
  47.   integer gargc ;
  48.   integer i ;
  49.   char **gargv ;
  50.   initialize () { integer i ; 
  51.     printf ( "This is PXtoPK, C Version 2.1\n" ) ; 
  52.     for ( i = 0 ; i <= 31 ; i ++ ) xchr [ i ] = '?' ; 
  53.       xchr [ 32 ] = ' ' ; 
  54.     xchr [ 33 ] = '!' ; 
  55.     xchr [ 34 ] = '"' ; 
  56.     xchr [ 35 ] = '#' ; 
  57.     xchr [ 36 ] = '$' ; 
  58.     xchr [ 37 ] = '%' ; 
  59.     xchr [ 38 ] = '&' ; 
  60.     xchr [ 39 ] = '\'' ; 
  61.     xchr [ 40 ] = '(' ; 
  62.     xchr [ 41 ] = ')' ; 
  63.     xchr [ 42 ] = '*' ; 
  64.     xchr [ 43 ] = '+' ; 
  65.     xchr [ 44 ] = ',' ; 
  66.     xchr [ 45 ] = '-' ; 
  67.     xchr [ 46 ] = '.' ; 
  68.     xchr [ 47 ] = '/' ; 
  69.     xchr [ 48 ] = '0' ; 
  70.     xchr [ 49 ] = '1' ; 
  71.     xchr [ 50 ] = '2' ; 
  72.     xchr [ 51 ] = '3' ; 
  73.     xchr [ 52 ] = '4' ; 
  74.     xchr [ 53 ] = '5' ; 
  75.     xchr [ 54 ] = '6' ; 
  76.     xchr [ 55 ] = '7' ; 
  77.     xchr [ 56 ] = '8' ; 
  78.     xchr [ 57 ] = '9' ; 
  79.     xchr [ 58 ] = ':' ; 
  80.     xchr [ 59 ] = ';' ; 
  81.     xchr [ 60 ] = '<' ; 
  82.     xchr [ 61 ] = '=' ; 
  83.     xchr [ 62 ] = '>' ; 
  84.     xchr [ 63 ] = '?' ; 
  85.     xchr [ 64 ] = '@' ; 
  86.     xchr [ 65 ] = 'A' ; 
  87.     xchr [ 66 ] = 'B' ; 
  88.     xchr [ 67 ] = 'C' ; 
  89.     xchr [ 68 ] = 'D' ; 
  90.     xchr [ 69 ] = 'E' ; 
  91.     xchr [ 70 ] = 'F' ; 
  92.     xchr [ 71 ] = 'G' ; 
  93.     xchr [ 72 ] = 'H' ; 
  94.     xchr [ 73 ] = 'I' ; 
  95.     xchr [ 74 ] = 'J' ; 
  96.     xchr [ 75 ] = 'K' ; 
  97.     xchr [ 76 ] = 'L' ; 
  98.     xchr [ 77 ] = 'M' ; 
  99.     xchr [ 78 ] = 'N' ; 
  100.     xchr [ 79 ] = 'O' ; 
  101.     xchr [ 80 ] = 'P' ; 
  102.     xchr [ 81 ] = 'Q' ; 
  103.     xchr [ 82 ] = 'R' ; 
  104.     xchr [ 83 ] = 'S' ; 
  105.     xchr [ 84 ] = 'T' ; 
  106.     xchr [ 85 ] = 'U' ; 
  107.     xchr [ 86 ] = 'V' ; 
  108.     xchr [ 87 ] = 'W' ; 
  109.     xchr [ 88 ] = 'X' ; 
  110.     xchr [ 89 ] = 'Y' ; 
  111.     xchr [ 90 ] = 'Z' ; 
  112.     xchr [ 91 ] = '[' ; 
  113.     xchr [ 92 ] = '\\' ; 
  114.     xchr [ 93 ] = ']' ; 
  115.     xchr [ 94 ] = '^' ; 
  116.     xchr [ 95 ] = '_' ; 
  117.     xchr [ 96 ] = '`' ; 
  118.     xchr [ 97 ] = 'a' ; 
  119.     xchr [ 98 ] = 'b' ; 
  120.     xchr [ 99 ] = 'c' ; 
  121.     xchr [ 100 ] = 'd' ; 
  122.     xchr [ 101 ] = 'e' ; 
  123.     xchr [ 102 ] = 'f' ; 
  124.     xchr [ 103 ] = 'g' ; 
  125.     xchr [ 104 ] = 'h' ; 
  126.     xchr [ 105 ] = 'i' ; 
  127.     xchr [ 106 ] = 'j' ; 
  128.     xchr [ 107 ] = 'k' ; 
  129.     xchr [ 108 ] = 'l' ; 
  130.     xchr [ 109 ] = 'm' ; 
  131.     xchr [ 110 ] = 'n' ; 
  132.     xchr [ 111 ] = 'o' ; 
  133.     xchr [ 112 ] = 'p' ; 
  134.     xchr [ 113 ] = 'q' ; 
  135.     xchr [ 114 ] = 'r' ; 
  136.     xchr [ 115 ] = 's' ; 
  137.     xchr [ 116 ] = 't' ; 
  138.     xchr [ 117 ] = 'u' ; 
  139.     xchr [ 118 ] = 'v' ; 
  140.     xchr [ 119 ] = 'w' ; 
  141.     xchr [ 120 ] = 'x' ; 
  142.     xchr [ 121 ] = 'y' ; 
  143.     xchr [ 122 ] = 'z' ; 
  144.     xchr [ 123 ] = '{' ; 
  145.     xchr [ 124 ] = '|' ; 
  146.     xchr [ 125 ] = '}' ; 
  147.     xchr [ 126 ] = '~' ; 
  148.     for ( i = 127 ; i <= 255 ; i ++ ) xchr [ i ] = '?' ; 
  149.       for ( i = 0 ; i <= 127 ; i ++ ) xord [ chr ( i ) ] = 32 ; 
  150.       for ( i = 32 ; i <= 126 ; i ++ ) xord [ xchr [ i ] ] = i ; 
  151.       pkloc = 0 ; 
  152.     strcpy(comment , " PXTOPK 2.1 output" ) ; 
  153.     power [ 0 ] = 1 ; 
  154.     for ( i = 1 ; i <= 30 ; i ++ ) power [ i ] = power [ i - 1 ] * 2 ; 
  155.       power [ 31 ] = - power [ i - 1 ] - power [ i - 1 ] ; 
  156.     } 
  157.   jumpout () { exit ( 1 ) ; 
  158.     } 
  159.   openpxlfile () { pxlfile = fopen ( pxlname , "r" ) ; 
  160.     } 
  161.   openpkfile () { pkfile = fopen ( pkname , "w" ) ; 
  162.     } 
  163.   integer pixelinteger () { integer i ; 
  164.     i = getc ( pxlfile ) ; 
  165.     if ( i > 127 ) i = i - 256 ; 
  166.     i = i * 256 + getc ( pxlfile ) ; 
  167.     i = i * 256 + getc ( pxlfile ) ; 
  168.     i = i * 256 + getc ( pxlfile ) ; 
  169.     return ( i ) ; 
  170.     } 
  171.   pkbyte ( b ) 
  172.   integer b ; 
  173.   { if ( b < 0 ) b = b + 256 ; 
  174.     putc ( b , pkfile ) ; 
  175.     incr ( pkloc ) ; 
  176.     } 
  177.   pkhalfword ( a ) 
  178.   integer a ; 
  179.   { if ( a < 0 ) a = a + 65536 ; 
  180.     pkbyte ( a / 256 ) ; 
  181.     pkbyte ( a % 256 ) ; 
  182.     } 
  183.   pkthreebytes ( a ) 
  184.   integer a ; 
  185.   { pkbyte ( a / 65536 % 256 ) ; 
  186.     pkbyte ( a / 256 % 256 ) ; 
  187.     pkbyte ( a % 256 ) ; 
  188.     } 
  189.   pkword ( a ) 
  190.   integer a ; 
  191.   { integer b ; 
  192.     if ( a < 0 ) 
  193.     { a = a + ( 1073741824 ) + ( 1073741824 ) ; 
  194.       b = 128 + a / 16777216 ; 
  195.       } 
  196.     else b = a / 16777216 ; 
  197.     pkbyte ( b ) ; 
  198.     pkbyte ( a / 65536 % 256 ) ; 
  199.     pkbyte ( a / 256 % 256 ) ; 
  200.     pkbyte ( a % 256 ) ; 
  201.     } 
  202.   pknyb ( a ) 
  203.   integer a ; 
  204.   { if ( bitweight == 16 ) 
  205.     { outputbyte = a * 16 ; 
  206.       bitweight = 1 ; 
  207.       } 
  208.     else 
  209.     { pkbyte ( outputbyte + a ) ; 
  210.       bitweight = 16 ; 
  211.       } 
  212.     } 
  213.   loadpxlfile () { /* 9997 9999 */ integer k ; 
  214.     openpxlfile () ; 
  215.     k = 0 ; 
  216.     if ( feof(pxlfile) ) goto lab9997 ; 
  217.     while ( ! feof(pxlfile) ) 
  218.       { mem [ k ] = pixelinteger () ; 
  219.         incr ( k ) ; 
  220.         if ( k > maxmemsize ) 
  221.         { printf ( " PXtoPK memory size exceeded on load of pixel file!\n" ) ;
  222.           jumpout () ; 
  223.           } 
  224.         } 
  225.       k-- ;
  226.     printf ( "%d bytes read from pixel file.\n" , ( 4 * k ) ) ; 
  227.     if ( k + 10000 > maxmemsize ) 
  228.     { printf ( " I don't think that there will be enough memory.\n" 
  229.       ) ; 
  230.       jumpout () ; 
  231.       } 
  232.     nextmemfree = k ; 
  233.     k = k - 1 ; 
  234.     if ( ( mem [ k ] != 1001 ) || ( mem [ 0 ] != 1001 ) ) goto lab9997 ; 
  235.     goto lab9999 ; 
  236.     lab9997 : 
  237.     { printf ( " PXL file is bad\n" ) ; 
  238.       jumpout () ; 
  239.       } 
  240.     lab9999 : dirptr = mem [ k - 1 ] ; 
  241.     } 
  242.   integer los ( a ) 
  243.   integer a ; 
  244.   { integer b ; 
  245.     b = lo ( a ) ; 
  246.     if ( b > 32767 ) return ( b - 65536 ) ; 
  247.     else return ( b ) ; 
  248.     } 
  249.   writepreamble () { integer i ; 
  250.     openpkfile () ; 
  251.     pkbyte ( 247 ) ; 
  252.     pkbyte ( 89 ) ; 
  253.     pkbyte ( 17 ) ; 
  254.     for ( i = 1 ; i <= 17 ; i ++ ) pkbyte ( xord [ comment [ i ] ] ) ; 
  255.       pkword ( mem [ dirptr + 514 ] ) ; 
  256.     pkword ( mem [ dirptr + 512 ] ) ; 
  257.     pkword ( hppp ) ; 
  258.     pkword ( hppp ) ; 
  259.     } 
  260.   writepostamble () { pkbyte ( 245 ) ; 
  261.     while ( ( pkloc % 4 != 0 ) ) pkbyte ( 246 ) ; 
  262.       printf ( "%d bytes written to packed file.\n" , pkloc ) ; 
  263.     } 
  264.   boolean equal ( row1 , row2 ) 
  265.   integer row1 , row2 ; 
  266.   { integer i ; 
  267.     boolean temp ; 
  268.     i = width ; 
  269.     temp = true ; 
  270.     while ( ( i > 0 ) && temp ) 
  271.       { if ( mem [ row1 ] != mem [ row2 ] ) temp = false ; 
  272.         incr ( row1 ) ; 
  273.         incr ( row2 ) ; 
  274.         i = i - 32 ; 
  275.         } 
  276.       return ( temp ) ; 
  277.     } 
  278.   shipcharacter () { integer craster ; 
  279.     integer wordwidth ; 
  280.     integer compsize ; 
  281.     integer horesc ; 
  282.     integer i, j, k ; 
  283.     integer zerorow ; 
  284.     integer onesrow ; 
  285.     integer repeatpointer ; 
  286.     integer bitcounts ; 
  287.     boolean bitssmaller ; 
  288.     integer finalsize ; 
  289.     integer count ; 
  290.     integer test ; 
  291.     integer curptr ; 
  292.     integer bit ; 
  293.     integer repeatflag ; 
  294.     integer word ; 
  295.     integer bitptr ; 
  296.     integer bitmod32 ; 
  297.     integer currepeat ; 
  298.     integer endraster ; 
  299.     integer dynf ; 
  300.     integer deriv[14] ; 
  301.     integer bcompsize ; 
  302.     boolean firston ; 
  303.     integer flagbyte ; 
  304.     boolean state ; 
  305.     boolean on ; 
  306.     integer hbit ; 
  307.     integer pbit ; 
  308.     boolean ron, son ; 
  309.     integer rcount, scount ; 
  310.     integer ri, si ; 
  311.     integer max2 ; 
  312.     integer predpkloc ; 
  313.     integer buff ; 
  314.     horesc = round ( pxlconv * mem [ dirptr + 3 ] ) ; 
  315.     width = hi ( mem [ dirptr ] ) ; 
  316.     height = lo ( mem [ dirptr ] ) ; 
  317.     cxoff = hi ( mem [ dirptr + 1 ] ) ; 
  318.     if (cxoff > 32767) cxoff -= 65536 ;
  319.     cyoff = los ( mem [ dirptr + 1 ] ) ; 
  320.     craster = mem [ dirptr + 2 ] ; 
  321.     wordwidth = ( width + 31 ) / 32 ; 
  322.     zerorow = nextmemfree ; 
  323.     onesrow = nextmemfree + wordwidth ; 
  324.     repeatpointer = onesrow + wordwidth ; 
  325.     bitcounts = repeatpointer + height + 1 ; 
  326.     for ( i = zerorow ; i <= onesrow - 1 ; i ++ ) mem [ i ] = 0 ; 
  327.       for ( i = onesrow ; i <= repeatpointer - 2 ; i ++ ) mem [ i ] = - 1 ; 
  328.       i = width % 32 ; 
  329.     if ( i == 0 ) mem [ repeatpointer - 1 ] = - 1 ; 
  330.     else if ( i == 1 ) mem [ repeatpointer - 1 ] = - ( 1073741824 ) - ( 
  331.     1073741824 ) ; 
  332.     else mem [ repeatpointer - 1 ] = - power [ 32 - i ] ; 
  333.     i = 0 ; 
  334.     j = height ; 
  335.     while ( i < j ) 
  336.       { if ( equal ( i * wordwidth + craster , zerorow ) ) mem [ 
  337.         repeatpointer + i ] = 0 ; 
  338.         else if ( equal ( i * wordwidth + craster , onesrow ) ) mem [ 
  339.         repeatpointer + i ] = 0 ; 
  340.         else if ( i + 1 == j ) mem [ repeatpointer + i ] = 0 ; 
  341.         else if ( equal ( i * wordwidth + craster , ( i + 1 ) * wordwidth + 
  342.         craster ) ) mem [ repeatpointer + i ] = 1 ; 
  343.         else mem [ repeatpointer + i ] = 0 ; 
  344.         incr ( i ) ; 
  345.         } 
  346.       i = 0 ; 
  347.     while ( i < j ) 
  348.       { k = i ; 
  349.         while ( mem [ repeatpointer + k ] == 1 ) incr ( k ) ; 
  350.           mem [ repeatpointer + i ] = k - i ; 
  351.         i = k + 1 ; 
  352.         } 
  353.       mem [ repeatpointer + i ] = 0 ; 
  354.     repeatflag = 0 ; 
  355.     bitptr = width - 1 ; 
  356.     currepeat = repeatpointer ; 
  357.     endraster = craster + height * wordwidth ; 
  358.     curptr = bitcounts ; 
  359.     count = 0 ; 
  360.     test = 0 ; 
  361.     do { incr ( bitptr ) ; 
  362.       if ( bitptr == width ) 
  363.       { bitmod32 = 0 ; 
  364.         bitptr = 0 ; 
  365.         if ( mem [ currepeat ] > 0 ) 
  366.         { repeatflag = mem [ currepeat ] ; 
  367.           currepeat = currepeat + repeatflag ; 
  368.           craster = craster + wordwidth * repeatflag ; 
  369.           } 
  370.         incr ( currepeat ) ; 
  371.         } 
  372.       decr ( bitmod32 ) ; 
  373.       if ( bitmod32 == - 1 ) 
  374.       { bitmod32 = 31 ; 
  375.         word = mem [ craster ] ; 
  376.         incr ( craster ) ; 
  377.         } 
  378.       if ( craster > endraster ) bit = 2 ; 
  379.       else if ( bitmod32 == 31 ) 
  380.       { if ( word < 0 ) 
  381.         { bit = 1 ; 
  382.           word = word + ( 1073741824 ) + ( 1073741824 ) ; 
  383.           } 
  384.         else bit = 0 ; 
  385.         } 
  386.       else 
  387.       { if ( word >= power [ bitmod32 ] ) 
  388.         { word = word - power [ bitmod32 ] ; 
  389.           bit = 1 ; 
  390.           } 
  391.         else bit = 0 ; 
  392.         } 
  393.       if ( bit == test ) incr ( count ) ; 
  394.       else 
  395.       { mem [ curptr ] = count ; 
  396.         incr ( curptr ) ; 
  397.         if ( curptr + 3 > maxmemsize ) 
  398.         { printf ( " Out of memory while saving character counts!\n" ) ;
  399.           jumpout () ; 
  400.           } 
  401.         count = 1 ; 
  402.         test = bit ; 
  403.         if ( repeatflag > 0 ) 
  404.         { mem [ curptr ] = - repeatflag ; 
  405.           repeatflag = 0 ; 
  406.           incr ( curptr ) ; 
  407.           } 
  408.         } 
  409.       } while ( ! ( test == 2 ) ) ; 
  410.     mem [ curptr ] = 0 ; 
  411.     mem [ curptr + 1 ] = 0 ; 
  412.     for ( i = 1 ; i <= 13 ; i ++ ) deriv [ i ] = 0 ; 
  413.       firston = mem [ bitcounts ] == 0 ; 
  414.     if ( firston ) incr ( bitcounts ) ; 
  415.     i = bitcounts ; 
  416.     compsize = 0 ; 
  417.     while ( mem [ i ] != 0 ) 
  418.       { j = mem [ i ] ; 
  419.         if ( j == - 1 ) incr ( compsize ) ; 
  420.         else 
  421.         { if ( j < 0 ) 
  422.           { incr ( compsize ) ; 
  423.             j = - j ; 
  424.             } 
  425.           if ( j < 209 ) compsize = compsize + 2 ; 
  426.           else 
  427.           { k = j - 193 ; 
  428.             while ( k >= 16 ) 
  429.               { k = k / 16 ; 
  430.                 compsize = compsize + 2 ; 
  431.                 } 
  432.               compsize = compsize + 1 ; 
  433.             } 
  434.           if ( j < 14 ) decr ( deriv [ j ] ) ; 
  435.           else if ( j < 209 ) incr ( deriv [ ( 223 - j ) / 15 ] ) ; 
  436.           else 
  437.           { k = 16 ; 
  438.             while ( ( k * 16 < j + 3 ) ) k = k * 16 ; 
  439.               if ( j - k <= 192 ) deriv [ ( 207 - j + k ) / 15 ] = deriv [ ( 
  440.             207 - j + k ) / 15 ] + 2 ; 
  441.             } 
  442.           } 
  443.         incr ( i ) ; 
  444.         } 
  445.       bcompsize = compsize ; 
  446.     dynf = 0 ; 
  447.     for ( i = 1 ; i <= 13 ; i ++ ) 
  448.       { compsize = compsize + deriv [ i ] ; 
  449.         if ( compsize <= bcompsize ) 
  450.         { bcompsize = compsize ; 
  451.           dynf = i ; 
  452.           } 
  453.         } 
  454.       compsize = ( bcompsize + 1 ) / 2 ; 
  455.     if ( ( compsize > ( height * width + 7 ) / 8 ) || ( height * width == 0 ) 
  456.     ) 
  457.     { compsize = ( height * width + 7 ) / 8 ; 
  458.       dynf = 14 ; 
  459.       } 
  460.     flagbyte = dynf * 16 ; 
  461.     if ( firston ) flagbyte = flagbyte + 8 ; 
  462.     if ( ( mem [ dirptr + 3 ] > 16777215 ) || ( mem [ dirptr + 3 ] < 0 ) || ( 
  463.     horesc < 0 ) || ( compsize > 196579 ) || ( width > 65535 ) || ( height > 
  464.     65535 ) || ( cxoff > 32767 ) || ( cyoff > 32767 ) || ( cxoff < - 32768 ) 
  465.     || ( cyoff < - 32768 ) ) 
  466.     { flagbyte = flagbyte + 7 ; 
  467.       pkbyte ( flagbyte ) ; 
  468.       compsize = compsize + 28 ; 
  469.       pkword ( compsize ) ; 
  470.       pkword ( car ) ; 
  471.       predpkloc = pkloc + compsize ; 
  472.       pkword ( mem [ dirptr + 3 ] ) ; 
  473.       pkword ( horesc * 65536 ) ; 
  474.       pkword ( 0 ) ; 
  475.       pkword ( width ) ; 
  476.       pkword ( height ) ; 
  477.       pkword ( cxoff ) ; 
  478.       pkword ( cyoff ) ; 
  479.       } 
  480.     else if ( ( horesc > 255 ) || ( width > 255 ) || ( height > 255 ) || ( 
  481.     cxoff > 127 ) || ( cyoff > 127 ) || ( cxoff < - 128 ) || ( cyoff < - 128 
  482.     ) || ( compsize > 1016 ) ) 
  483.     { compsize = compsize + 13 ; 
  484.       flagbyte = flagbyte + compsize / 65536 + 4 ; 
  485.       pkbyte ( flagbyte ) ; 
  486.       pkhalfword ( compsize % 65536 ) ; 
  487.       pkbyte ( car ) ; 
  488.       predpkloc = pkloc + compsize ; 
  489.       pkthreebytes ( mem [ dirptr + 3 ] ) ; 
  490.       pkhalfword ( horesc ) ; 
  491.       pkhalfword ( width ) ; 
  492.       pkhalfword ( height ) ; 
  493.       pkhalfword ( cxoff ) ; 
  494.       pkhalfword ( cyoff ) ; 
  495.       } 
  496.     else 
  497.     { compsize = compsize + 8 ; 
  498.       flagbyte = flagbyte + compsize / 256 ; 
  499.       pkbyte ( flagbyte ) ; 
  500.       pkbyte ( compsize % 256 ) ; 
  501.       pkbyte ( car ) ; 
  502.       predpkloc = pkloc + compsize ; 
  503.       pkthreebytes ( mem [ dirptr + 3 ] ) ; 
  504.       pkbyte ( horesc ) ; 
  505.       pkbyte ( width ) ; 
  506.       pkbyte ( height ) ; 
  507.       pkbyte ( cxoff ) ; 
  508.       pkbyte ( cyoff ) ; 
  509.       } 
  510.     if ( dynf != 14 ) 
  511.     { bitweight = 16 ; 
  512.       max2 = 208 - 15 * dynf ; 
  513.       i = bitcounts ; 
  514.       while ( mem [ i ] != 0 ) 
  515.         { j = mem [ i ] ; 
  516.           if ( j == - 1 ) pknyb ( 15 ) ; 
  517.           else 
  518.           { if ( j < 0 ) 
  519.             { pknyb ( 14 ) ; 
  520.               j = - j ; 
  521.               } 
  522.             if ( j <= dynf ) pknyb ( j ) ; 
  523.             else if ( j <= max2 ) 
  524.             { j = j - dynf - 1 ; 
  525.               pknyb ( j / 16 + dynf + 1 ) ; 
  526.               pknyb ( j % 16 ) ; 
  527.               } 
  528.             else 
  529.             { j = j - max2 + 15 ; 
  530.               k = 16 ; 
  531.               while ( k <= j ) 
  532.                 { k = k * 16 ; 
  533.                   pknyb ( 0 ) ; 
  534.                   } 
  535.                 while ( k > 1 ) 
  536.                 { k = k / 16 ; 
  537.                   pknyb ( j / k ) ; 
  538.                   j = j % k ; 
  539.                   } 
  540.                 } 
  541.             } 
  542.           incr ( i ) ; 
  543.           } 
  544.         if ( bitweight != 16 ) pkbyte ( outputbyte ) ; 
  545.       } 
  546.     else 
  547.     { buff = 0 ; 
  548.       pbit = 8 ; 
  549.       i = bitcounts ; 
  550.       hbit = width ; 
  551.       on = ! firston ; 
  552.       state = false ; 
  553.       count = 0 ; 
  554.       repeatflag = 0 ; 
  555.       while ( ( mem [ i ] != 0 ) || state || ( count > 0 ) ) 
  556.         { if ( state ) 
  557.           { count = rcount ; 
  558.             i = ri ; 
  559.             on = ron ; 
  560.             decr ( repeatflag ) ; 
  561.             } 
  562.           else 
  563.           { rcount = count ; 
  564.             ri = i ; 
  565.             ron = on ; 
  566.             } 
  567.           do { if ( count == 0 ) 
  568.             { if ( mem [ i ] < 0 ) 
  569.               { if ( ! state ) repeatflag = - mem [ i ] ; 
  570.                 incr ( i ) ; 
  571.                 } 
  572.               count = mem [ i ] ; 
  573.               incr ( i ) ; 
  574.               on = ! on ; 
  575.               } 
  576.             if ( ( count >= pbit ) && ( pbit < hbit ) ) 
  577.             { if ( on ) buff = buff + power [ pbit ] - 1 ; 
  578.               pkbyte ( buff ) ; 
  579.               buff = 0 ; 
  580.               hbit = hbit - pbit ; 
  581.               count = count - pbit ; 
  582.               pbit = 8 ; 
  583.               } 
  584.             else if ( ( count < pbit ) && ( count < hbit ) ) 
  585.             { if ( on ) buff = buff + power [ pbit ] - power [ pbit - count ] 
  586.               ; 
  587.               pbit = pbit - count ; 
  588.               hbit = hbit - count ; 
  589.               count = 0 ; 
  590.               } 
  591.             else 
  592.             { if ( on ) buff = buff + power [ pbit ] - power [ pbit - hbit ] 
  593.               ; 
  594.               count = count - hbit ; 
  595.               pbit = pbit - hbit ; 
  596.               hbit = width ; 
  597.               if ( pbit == 0 ) 
  598.               { pkbyte ( buff ) ; 
  599.                 buff = 0 ; 
  600.                 pbit = 8 ; 
  601.                 } 
  602.               } 
  603.             } while ( ! ( hbit == width ) ) ; 
  604.           if ( state && ( repeatflag == 0 ) ) 
  605.           { count = scount ; 
  606.             i = si ; 
  607.             on = son ; 
  608.             state = false ; 
  609.             } 
  610.           else if ( ! state && ( repeatflag > 0 ) ) 
  611.           { scount = count ; 
  612.             si = i ; 
  613.             son = on ; 
  614.             state = true ; 
  615.             } 
  616.           } 
  617.         if ( pbit != 8 ) pkbyte ( buff ) ; 
  618.       } 
  619.     if ( predpkloc != pkloc ) 
  620.     { printf ( " Bad predicted character length: character %d\n" , car ) ;
  621.       jumpout () ; 
  622.       } 
  623.     } 
  624.   dialog () { integer i ; 
  625.     if ( ( gargc < 2 ) || ( gargc > 3 ) ) 
  626.     { printf ( " Usage: pxtopk pxlfile[.pxl] [pkfile[.pk]]\n" ) ; 
  627.       jumpout () ; 
  628.       } 
  629.     strcpy ( pxlname , gargv [ 1 ] ) ; 
  630.     lastext = - 1 ; 
  631.     i = 0 ; 
  632.     while ( pxlname [ ++ i ] != 0 ) if ( pxlname [ i ] == '.' ) lastext = i ; 
  633.       else if ( pxlname [ i ] == '/' ) lastext = - 1 ; 
  634.       if ( lastext == - 1 ) 
  635.     { strcpy ( pxlname + i , ".pxl" ) ; 
  636.       lastext = i ; 
  637.       } 
  638.     if ( gargc == 3 ) 
  639.     { strcpy ( pkname , gargv [ 2 ] ) ; 
  640.       haveext = true ; 
  641.       } 
  642.     else 
  643.     { strcpy ( pkname , pxlname ) ; 
  644.       haveext = false ; 
  645.       } 
  646.     } 
  647.   main (argc, argv) 
  648. int argc ;
  649. char *argv[] ;
  650. { gargc = argc ;
  651.   gargv = argv ;
  652.   initialize () ; 
  653.   dialog () ; 
  654.   loadpxlfile () ; 
  655.   pxlconv = ( mem [ dirptr + 514 ] / 16 ) / 65536.0 * mem [ dirptr + 513 ] / 
  656.   72.27 / 5242880.0 ; 
  657.   hppp = round ( mem [ dirptr + 513 ] * 65536 / 72.27 / 5 ) ; 
  658.   i = round ( hppp * 72.27 / 65536 ) ;
  659.   if ( ! haveext ) sprintf ( pkname + lastext , ".%dpk" , i ) ;
  660.   writepreamble () ; 
  661.   for ( car = 0 ; car <= 127 ; car ++ ) 
  662.     { if ( mem [ dirptr + 2 ] != 0 ) shipcharacter () ; 
  663.       dirptr = dirptr + 4 ; 
  664.       } 
  665.     writepostamble () ; 
  666.   lab9999 : ; 
  667.   } 
  668.